//
//  ZZHForTD_UPLoadingView.m
//  GJProgressView
//
//  Created by Star J on 2018/4/28.
//  Copyright © 2018年 GJ. All rights reserved.
//

#import "ZZHForTD_UPLoadingView.h"


#define DEFAULTSIZE 75.f

@interface ZZHForTD_UPLoadingView ()
@property (nonatomic ,assign)CGFloat progress;
@property (nonatomic,strong) CAGradientLayer *grain;
@property (nonatomic,strong) UIView *PathView;
@property (nonatomic , strong)UILabel *defaultLabel,*numLabel;

@end

@implementation ZZHForTD_UPLoadingView


-(instancetype)initWithFrame:(CGRect)frame{
    if(self){
        self = [super initWithFrame:frame];
        [self create_selfViews];
    }
    return self;
}

-(void)create_selfViews{
    dispatch_async(dispatch_get_main_queue(), ^{
        [self addSubview:self.defaultLabel];
        [self addSubview:self.numLabel];
    });
    
}

-(void)set_Progress:(CGFloat)ProgressValue{
    
    
    
    UIBezierPath *path = [UIBezierPath bezierPathWithArcCenter:CGPointMake(self.bounds.size.width/2, self.bounds.size.height/2) radius:self.bounds.size.width/2 - _strokelineWidth startAngle:-M_PI/2.f endAngle:(M_PI*2)-M_PI/2.f clockwise:YES];
    
    self.backgroundLine.path = path.CGPath;
    self.mainLine.path = path.CGPath;
    self.mainLine.strokeStart = 0.f;
    self.mainLine.strokeEnd = ProgressValue/100.f;
}

-(void)setText_defaultText:(NSString*)defaultText andPageText:(NSString*)PageText{
    self.defaultLabel.text = defaultText;
    self.numLabel.text = PageText;
}


- (CAShapeLayer *)backgroundLine {
    if(_backgroundLine == nil) {
        _backgroundLine = [[CAShapeLayer alloc] init];
        _backgroundLine.fillColor = [UIColor clearColor].CGColor;
        _backgroundLine.strokeColor = [UIColor whiteColor].CGColor;
        _backgroundLine.lineWidth = _strokelineWidth;
        [self.layer addSublayer:_backgroundLine];
    }
    return _backgroundLine;
}

- (CAShapeLayer *)mainLine {
    if(_mainLine == nil) {
        _mainLine = [[CAShapeLayer alloc] init];
        _mainLine.fillColor = [UIColor clearColor].CGColor;
        _mainLine.strokeColor = RGBA(244, 103, 82, 1).CGColor;
        _mainLine.lineWidth = _strokelineWidth;
        [self.layer addSublayer:_mainLine];
        
    }
    return _mainLine;
}


//- (CAGradientLayer *)grain {
//    if(_grain == nil) {
//        _grain = [[CAGradientLayer alloc] init];
//        _grain.frame = CGRectMake(0, 0, self.bounds.size.width,self.bounds.size.height);
//        [_grain setColors:[NSArray arrayWithObjects:
//                           (id)[[self colorWithHexString:@"25e283"] CGColor],
//                           (id)[[self colorWithHexString:@"22cc9f"] CGColor],
//                           (id)[[self colorWithHexString:@"29babd"] CGColor],
//                           (id)[[self colorWithHexString:@"2facd4"] CGColor],
//                           (id)[[self colorWithHexString:@"4299fa"] CGColor],
//                           nil]];
//        [_grain setLocations:@[@0, @0.3, @0.7, @1]];
//        [_grain setStartPoint:CGPointMake(0, 0)];
//        [_grain setEndPoint:CGPointMake(1, 0)];
//        _grain.type = kCAGradientLayerAxial;
//        [self.layer addSublayer:_grain];
//    }
//    return _grain;
//}

-(UILabel*)defaultLabel{
    if(!_defaultLabel){
        _defaultLabel = [[UILabel alloc]initWithFrame:CGRectMake(0, 0, 70, 15)];
        _defaultLabel.center = CGPointMake(self.frame.size.width/2, self.frame.size.height/2 - 6);
        _defaultLabel.textAlignment = NSTextAlignmentCenter;
        _defaultLabel.textColor = [UIColor whiteColor];
        _defaultLabel.font = [UIFont systemFontOfSize:12.5 weight:0.001];
//        _defaultLabel.text = @"loading";
    }
    return _defaultLabel;
}

-(UILabel *)numLabel{
    if(!_numLabel){
        _numLabel = [[UILabel alloc]initWithFrame:CGRectMake(0, 0, 70, 15)];
        _numLabel.center = CGPointMake(self.frame.size.width/2, self.frame.size.height/2 + 14);
        _numLabel.textAlignment = NSTextAlignmentCenter;
        _numLabel.textColor = [UIColor whiteColor];
        _numLabel.font = [UIFont systemFontOfSize:12.7 weight:0.001];
//        _numLabel.text = @"1/5";
    }
    return _numLabel;
}




#pragma mark - 方法 Methods
//- (UIColor *)colorWithHexString: (NSString *)color
//{
//    NSString *cString = [[color stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]] uppercaseString];
//
//    // String should be 6 or 8 characters
//    if ([cString length] < 6) {
//        return [UIColor clearColor];
//    }
//
//    // strip 0X if it appears
//    if ([cString hasPrefix:@"0X"])
//        cString = [cString substringFromIndex:2];
//    if ([cString hasPrefix:@"#"])
//        cString = [cString substringFromIndex:1];
//    if ([cString length] != 6)
//        return [UIColor clearColor];
//
//    // Separate into r, g, b substrings
//    NSRange range;
//    range.location = 0;
//    range.length = 2;
//
//    //r
//    NSString *rString = [cString substringWithRange:range];
//
//    //g
//    range.location = 2;
//    NSString *gString = [cString substringWithRange:range];
//
//    //b
//    range.location = 4;
//    NSString *bString = [cString substringWithRange:range];
//
//    // Scan values
//    unsigned int r, g, b;
//    [[NSScanner scannerWithString:rString] scanHexInt:&r];
//    [[NSScanner scannerWithString:gString] scanHexInt:&g];
//    [[NSScanner scannerWithString:bString] scanHexInt:&b];
//
//    return [UIColor colorWithRed:((float) r / 255.0f) green:((float) g / 255.0f) blue:((float) b / 255.0f) alpha:1.0f];
//}

//-(void)stop{
//
//}

@end
